package com.keqing.tomato;

import static com.keqing.tomato.fragment.FragmentSettings.request_code_sound_focus;
import static com.keqing.tomato.fragment.FragmentSettings.request_code_sound_rest;
import static com.keqing.tomato.fragment.FragmentSettings.request_code_time_focus;
import static com.keqing.tomato.fragment.FragmentSettings.request_code_time_rest;
import static com.keqing.tomato.util.PreferenceUtil.MySetting.focus_time_array;
import static com.keqing.tomato.util.PreferenceUtil.MySetting.rest_time_array;
import static com.keqing.tomato.util.PreferenceUtil.MySetting.soundList;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.keqing.tomato.adapter.RadioListAdapter;
import com.keqing.tomato.bean.Sound;
import com.keqing.tomato.util.PreferenceUtil;
import com.keqing.tomato.util.PreferenceUtil.MySetting;
import com.keqing.tomato.util.SoundUtil;

import java.util.ArrayList;
import java.util.List;

public class RadioListActivity extends AppCompatActivity {
    private MySetting mySetting;
    private int currentPos;
    private final List<String> stringList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("com/keqing/tomato", "RadioListActivity - onCreate()");
        setContentView(R.layout.activity_radio_list);
        mySetting = PreferenceUtil.getMySetting(this);

        TextView mTitleTv = findViewById(R.id.title);
        TextView go_back = findViewById(R.id.go_back);
        go_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(1, new Intent());
                RadioListActivity.this.finish();
            }
        });
        mTitleTv.setText(getIntent().getStringExtra("title"));
        int requestCode = getIntent().getIntExtra("requestCode", -1);

        if (requestCode == request_code_sound_focus) {
            loadData(mySetting.getFocusSound());
            mTitleTv.setText(R.string.focus_time_finish_sound);
        }else if (requestCode == request_code_sound_rest) {
            loadData(mySetting.getRestSound());
            mTitleTv.setText(R.string.rest_time_finish_sound);
        }else if (requestCode == request_code_time_focus) {
            loadData2(mySetting.getFocusTime(), focus_time_array);
            mTitleTv.setText(R.string.tomato_time_length);
        }else if (requestCode == request_code_time_rest) {
            loadData2(mySetting.getRestTime(), rest_time_array);
            mTitleTv.setText(R.string.rest_time_length);
        }

        RadioListAdapter<String> adapter = new RadioListAdapter<>(stringList, currentPos, new RadioListAdapter.OnItemClickListener() {
            @Override
            public void onClick(int pos) {
                switch (requestCode) {
                    case request_code_sound_focus:
                        Sound sound = soundList.get(pos);
                        mySetting.setFocusSoundPath(sound.getFilePath());
                        mySetting.setFocusSound(sound.getCnName());
                        SoundUtil.startMedia(sound.getFilePath(), RadioListActivity.this);
                        break;
                    case request_code_sound_rest:
                        sound = soundList.get(pos);
                        mySetting.setRestSoundPath(sound.getFilePath());
                        mySetting.setRestSound(sound.getCnName());
                        SoundUtil.startMedia(sound.getFilePath(), RadioListActivity.this);
                        break;
                    case request_code_time_focus:
                        mySetting.setFocusTime(focus_time_array[pos]);
                        break;
                    case request_code_time_rest:
                        mySetting.setRestTime(rest_time_array[pos]);
                        break;
                    default:
                        throw new IllegalStateException("Unexpected value: " + requestCode);
                }
                setResult(1, new Intent());
            }
        });
        RecyclerView recycler_view = findViewById(R.id.recycler_view);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recycler_view.setLayoutManager(manager);
        recycler_view.setAdapter(adapter);
    }


    public void loadData(String currentSound){
        for (int i = 0; i < soundList.size(); i++) {
            stringList.add(soundList.get(i).getCnName());
            if(soundList.get(i).getCnName().equals(currentSound)){
                currentPos = i;
            }
        }
    }

    public void loadData2(int currentTime,int[] data){
        for (int i = 0; i < data.length; i++) {
            stringList.add(data[i] + "分钟");
            if (data[i] == currentTime) {
                currentPos = i;
            }
        }
    }


    @Override
    public void finish() {
        super.finish();
        Log.e("com/keqing/tomato", "RadioListActivity - finish()");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("com/keqing/tomato", "RadioListActivity - onDestroy()");
        SoundUtil.releaseMedia();
    }
}